package com.ly.hotel.view.setting;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.ly.hotel.R;
import com.ly.hotel.common.SharedPreferencesConfig;
import com.ly.hotel.util.ToastUtil;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SendBroadcastLayout extends LinearLayout implements View.OnClickListener {

    private String host = "238.9.9.1";// 广播地址
    private int port = 8302;// 广播的目的端口
    private String message = "";// 用于发送的字符串
    private static boolean isBroadcast = false;
    private Context mContext;
    private EditText mMessageEdit;
    private Button mSendBroadcast;
    private Button mStopBroadcast;

    public SendBroadcastLayout(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.layout_send_broadcast, this);
        initView();
    }

    private void initView() {
        mMessageEdit = findViewById(R.id.message_edit);
        mMessageEdit.setText(SharedPreferencesConfig.getSpaServer(mContext));
        mSendBroadcast = findViewById(R.id.send_broadcast_btn);
        mStopBroadcast = findViewById(R.id.stop_broadcast_btn);
        mSendBroadcast.setOnClickListener(this);
        mStopBroadcast.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_broadcast_btn:
                sendBroadcast();
                break;

            case R.id.stop_broadcast_btn:
                stopBroadcast();
                break;
        }
    }

    /**
     * 发送广播
     */
    private void sendBroadcast() {
        message = mMessageEdit.getText().toString().replaceAll("\\s*|\t|\r|\n", "");
        if (TextUtils.isEmpty(message)) {
            ToastUtil.showShortToast(mContext, "SPA服务器地址不能为空！", 3000);
        } else {
            if (!message.contains(":")) {
                ToastUtil.showShortToast(mContext, "SPA服务器地址不合法！", 3000);
            } else {
                if (isBroadcast) {
                    ToastUtil.showShortToast(mContext, "已经在发送广播！", 3000);
                } else {
                    isBroadcast = true;
                    message = "serverip:" + message;
                    ToastUtil.showShortToast(mContext, "发送广播！", 3000);
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                InetAddress address = InetAddress.getByName(host);
                                DatagramSocket ds = new DatagramSocket();
                                DatagramPacket dp = new DatagramPacket(message.getBytes(), message.length(), address, port);
                                while (isBroadcast) {
                                    ds.send(dp);
                                    try {
                                        Log.i("broadcast", "发送广播！");
                                        Thread.sleep(3000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                Log.i("broadcast", "停止广播！");
                            } catch (UnknownHostException e) {
                                e.printStackTrace();
                            } catch (SocketException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }.start();
                }
            }
        }
    }

    /**
     * 停止广播
     */
    private void stopBroadcast() {
        ToastUtil.showShortToast(mContext, "停止广播！", 3000);
        isBroadcast = false;
    }

    @Override
    protected void onDetachedFromWindow() {
        if (isBroadcast) {
            isBroadcast = false;
        }
        super.onDetachedFromWindow();
    }
}
